import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 配合线程池使用
 */
public class TestFutureTask2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始执行...");

        // 创建一个5个线程的线程池
        // ExecutorService executorService = Executors.newFixedThreadPool(5);

        // 自定义的线程池
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(10, 50, 5,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(50), new ThreadPoolExecutor.CallerRunsPolicy());

        List<FutureTask<Integer>> taskList = new LinkedList<>();

        for (int i = 0; i < 20; i++) {
            CallableExample callableExample = new CallableExample();
            FutureTask<Integer> futureTask = new FutureTask<>(callableExample);
            taskList.add(futureTask);
            executorService.submit(futureTask);
        }

        System.out.println("主线程继续干其他事...");

        for (FutureTask<Integer> task : taskList) {
            Integer result = task.get();
            System.out.println("结果是:" + result);
        }

        executorService.shutdown();
    }

}
